(function() {

	var appModule = angular.module('tracecashModule');

	appModule.directive('sinisterGeneral', function() {
		return {
			restrict : 'AE',
			templateUrl : 'html/general/sinister-general.html',
			controller : 'SinisterGeneralCtrl',
			require : '^ngModel',
			replace : 'false',
			scope : {
				sinister : '=ngModel',
				config : '='
			}
		};
	});

	appModule.directive('sinisterGeneralCapital', function() {
		return {
			restrict : 'AE',
			templateUrl : 'html/general/sinister-general-capital.html',
			controller : 'SinisterGeneralCapitalCtrl',
			require : '^ngModel',
			replace : 'false',
			scope : {
				sinister : '=ngModel'
			}
		};
	});

	appModule.directive('sinisterGeneralFees', function() {
		return {
			restrict : 'AE',
			templateUrl : 'html/general/sinister-general-fees.html',
			require : '^ngModel',
			controller : 'SinisterGeneralFeesCtrl',
			replace : 'false',
			scope : {
				sinister : '=ngModel'
			}
		};
	});

	appModule.controller('SinisterGeneralCtrl', ['$scope', function ($scope) {
		if ($scope.sinister != null && $scope.sinister.id == null) {
			$scope.$watch('sinister != null && sinister.sinisterInfo.estimatedAmount', function(value) {
				if ($scope.sinister.sinisterInfo != null) {
					$scope.sinister.sinisterInfo.currentReserve = value;
					$scope.sinister.sinisterInfo.totalReserve = value;
					$scope.sinister.sinisterInfo.initialReserve = value;
				}
			});
		}
	}]);

	appModule.controller('SinisterGeneralCapitalCtrl', ['$scope', function ($scope) {
		if ($scope.sinister.id == null) {
			$scope.$watch('(sinister.saldoGrabacion * sinister.sinisterInfo.debitEffectiveExcess) / sinister.sinisterInfo.fundingAmount', function(value) {
				if ($scope.sinister.sinisterInfo != null) {
					$scope.sinister.sinisterInfo.estimatedAmount = value;
				}
			});
		}
	}]);

	appModule.controller('SinisterGeneralFeesCtrl', ['$scope', function ($scope) {
		if ($scope.sinister.id == null) {
			$scope.$watch('sinister.months * sinister.cuoteImport', function(value) {
				if ($scope.sinister.sinisterInfo != null) {
					$scope.sinister.sinisterInfo.estimatedAmount = value;
				}
			});
		}
	}]);

})();